package com.yw.atp.model;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.yw.atp.Constant;
import com.yw.atp.util.DateUtil;


@Entity
@Table(name=Constant.DB_PREFIX+"appointment")
@NamedQueries({
    @NamedQuery(name = "Appointment.getAreaAppointment", 
        query = "select a FROM Appointment a where a.beginDate between :beginDate and :endDate or a.endDate between :beginDate and :endDate or(a.beginDate <= :beginDate and a.endDate >= :endDate)"
    )
})
public class Appointment implements IDEntity{
	private static final long serialVersionUID = 1L;

	@Id
    @GeneratedValue
    private Integer id;
	
	@Temporal(TemporalType.TIMESTAMP)
	private Date beginDate;
	
	@Temporal(TemporalType.TIMESTAMP)
	private Date endDate;
	
	private String subject;
	
	private String location;
	private String status = "0";;
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
    
	@Temporal(TemporalType.TIMESTAMP)
	public Date getBeginDate() {
		return beginDate;
	}

	@Temporal(TemporalType.TIMESTAMP)
	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}

	@Temporal(TemporalType.TIMESTAMP)
	public Date getEndDate() {
		return endDate;
	}

	@Temporal(TemporalType.TIMESTAMP)
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}
    
	public Appointment(Date beginDate, Date endDate, String subject,
			String location) {
		super();
		this.beginDate = beginDate;
		this.endDate = endDate;
		this.subject = subject;
		this.location = location;
	}
	
	public Appointment() {
		super();
	}
	
	public static JSONArray capture(List<Appointment> appms) throws JSONException{
		JSONArray jsonAppms = new JSONArray();
		for(Appointment appm : appms){
			jsonAppms.put(appm.capture());
		}
		return jsonAppms;
	}
	
	public JSONObject capture() throws JSONException{
		JSONObject appm = new JSONObject();
		appm.put("f_t",DateUtil.DateFormat(this.beginDate,null));
		appm.put("t_t",DateUtil.DateFormat(this.endDate,null));
		appm.put("s",this.subject);
		appm.put("loc",this.location);
		appm.put("p_id",this.id);
		return appm;
	}
}
